<template>
  <div class="order-item">
    <!-- 头部区域 -->
    <div class="head">
      <span class="time">下单时间: {{ order.createTime }}</span>
      <span class="order-number">订单编号: {{ order.id }}</span>
      <!-- 倒计时显示逻辑, orderState 状态为1 -> 待付款 -->
      <span v-if="order.orderState === 1" class="countdown">
            <i class="iconfont icon-down-time"></i>
            <b>付款时间: <span>{{ countdownText }}</span></b>
      </span>
      <!-- 删除订单显示逻辑 5-> 已完成, 6 -> 已取消 -->
      <a v-if="[5, 6].includes(order.orderState)"
         class="del"
         href="javascript:;"
         @click="$emit('on-delete', order)">删除</a>
    </div>

    <!-- 主体内容 -->
    <div class="body">
      <div class="column goods">
        <ul>
          <li v-for="item in order.skus" :key="item.id">
            <router-link :to="`/product/${item.spuId}`" class="image">
              <img v-lazy="item.image" alt=""/>
            </router-link>
            <div class="info">
              <p class="name ellipsis-2">{{ item.name }}</p>
              <p class="attr ellipsis">
                <span>{{ item.attrsText }}</span>
              </p>
            </div>
            <div class="price">¥{{ item.realPay }}</div>
            <div class="count">x{{ item.quantity }}</div>
          </li>
        </ul>
      </div>
      <div class="column state">
        <!-- 根据索引去orderStatus常量数据, 取数据 -->
        <p>{{ orderStatus[order.orderState].label }}</p>
        <!-- 待收货 查看物流 -->
        <p v-if="order.orderState === 3">
          <a class="green"
             href="javascript:;"
             @click="$emit('on-logistics', order)">查看物流
          </a>
        </p>

        <!-- 待评价 评价商品 -->
        <p v-if="order.orderState === 4"><a class="green" href="javascript:;">评价商品</a></p>

        <!-- 已完成 查看评价 -->
        <p v-if="order.orderState === 5"><a class="green" href="javascript:;">查看评价</a></p>
      </div>
      <div class="column amount">
        <p class="red">¥{{ order.payMoney }}</p>
        <p>（含运费：¥{{ order.postFee }}）</p>
        <p>在线支付</p>
      </div>
      <!-- v-if="[1].includes(order.orderState)" -->
      <div class="column action">
        <XtxButton v-if="order.orderState === 1"
                   size="small"
                   type="primary"
                   @click="$router.push(`/member/pay?orderId=${order.id}`)">立即付款
        </XtxButton>
        <XtxButton v-if="order.orderState === 3"
                   size="small"
                   type="primary"
                   @click="$emit('on-confirm', order)">确认收货
        </XtxButton>
        <p><a href="javascript:;" @click="$router.push(`/member/order/${order.id}`)">查看详情</a></p>

        <!-- 点击取消订单时, 触发自定义事件, 由父组件来弹出确认框. 这样父组件只要维护一个弹出框就行 -->
        <!-- 如果把弹出框定义在OrderItem中, 那么的话, 有多少订单, 就会有多少个弹出框 -->
        <p v-if="order.orderState === 1"
           @click="$emit('on-cancel', order)">
          <a href="javascript:;">取消订单</a>
        </p>

        <p v-if="[2, 3, 4, 5, 6].includes(order.orderState)"
           @click="$router.push(`/member/checkout?orderId=${order.id}`)">
          <a href="javascript:;">再次购买</a>
        </p>
        <p v-if="[4, 5].includes(order.orderState)"><a href="javascript:;">申请售后</a></p>
      </div>
    </div>
  </div>
</template>

<script>
import { usePayTime } from '@/hooks'
import { orderStatus } from '@/constant'

export default {
  name: 'OrderItem',
  props: {
    order: {
      type: Object,
      default: () => ({})   // 默认值为对象的写法
    }
  },
  emits: ['on-cancel', 'on-delete', 'on-confirm', 'on-logistics'],
  setup(props) {
    // 使用倒计时方法
    const {
      start,
      countdownText,
      time
    } = usePayTime()

    // 开启定时器
    if (props.order.countdown > -1) {
      start(props.order.countdown)
    }

    return {
      countdownText,
      time,
      orderStatus
    }
  }
}
</script>

<style lang="less" scoped>
.order-item {
  //height: 183px;
  border: 1px solid #f5f5f5;
  margin-bottom: 20px;

  .head {
    height: 50px;
    line-height: 50px;
    padding: 0 20px;
    overflow: hidden;
    background-color: #f5f5f5;

    .order-number {
      margin-left: 20px;
    }

    .countdown {
      float: right;
      // 注意这 i标签和b标签两个都要加 vertical-align 才能实现对齐方式
      i {
        vertical-align: middle;
        margin-right: 3px;
      }

      b {
        vertical-align: middle;
        font-weight: normal;
      }
    }

    .del {
      margin-right: 0;
      float: right;
      color: #999;
    }
  }

  .body {
    display: flex;
    align-items: stretch;

    .column {
      border-left: 1px solid #f5f5f5;
      text-align: center;
      padding: 20px;

      > p {
        padding-top: 10px;
      }

      &:first-child {
        border-left: none;
      }

      &.goods {
        flex: 1;
        padding: 0;
        // 控制某个弹性盒子在侧轴的对齐方式
        align-self: center;

        li {
          display: flex;
          padding: 10px;
          border-bottom: 1px solid #f5f5f5;

          &:last-child {
            border-bottom: none;
          }

          .image {
            width: 70px;
            height: 70px;
            border: 1px solid #f5f5f5;

            img {
              width: 100%;
              height: 100%;
            }
          }

          .info {
            width: 220px;
            text-align: left;
            padding: 0 10px;

            p {
              margin-bottom: 5px;

              &.name {
                height: 38px;
              }

              &.attr {
                color: #999;
                font-size: 12px;

                span {
                  margin-right: 5px;
                }
              }
            }
          }

          .price {
            width: 100px;
          }

          .count {
            width: 80px;
          }
        }
      }

      &.state {
        width: 120px;

        .green {
          color: @xtxColor;
        }
      }

      &.amount {
        width: 200px;

        .red {
          color: @priceColor;
        }
      }

      &.action {
        width: 140px;

        a {
          display: block;

          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
</style>
